package com.carlinfo.myjdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
public class JdbcMain
{
	public static void main(String[] args)
	{
		String url = "jdbc:mysql://localhost:3306/student?useSSL=false";
		String sql = "select * from table_1;";
		String user = "root";
		String password = "123456";
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		try
		{
			/* 加载驱动的第一种方式 */
//			Driver driver = new Driver();
			/* 加载驱动的第二种方式 */
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager.getConnection(url, user, password);
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			System.out.println("id\t性别\t金钱\t发布时间");
			while(rs.next()) 
			{
				int id = rs.getInt("id");
				String sex = rs.getString("sex");
				String name = rs.getString("name");
				int money = rs.getInt("money");
				Date publicTime = rs.getTimestamp("publicTime");
				System.out.println(id+"\t"+sex+"\t"+name+"\t\t\t"+money+"\t"+publicTime);
			}	
		} catch (SQLException e)
		{
			e.printStackTrace();
		} catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		} finally 
		{
			try
			{
				if(rs!=null)
				{
					rs.close();
					rs = null;
				}
				
			} catch (SQLException e)
			{
				e.printStackTrace();
			}
			try
			{
				if(statement!=null)
				{
					statement.close();
					statement = null;
				}
				
			} catch (SQLException e1)
			{
				e1.printStackTrace();
			}
			try
			{
				if(connection!=null)
				{
					connection.close();
					connection = null;
				}
				
			} catch (SQLException e2)
			{
				e2.printStackTrace();
			}
		}
	}
}

